/*
 * BaseBean.java
 *
 * Created on 12 de Outubro de 2007, 11:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package br.uff.javaavancado.controller;


import br.uff.javaavancado.util.BeanUtils;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
//import org.apache.log4j.Logger;

public class BaseController {


    /**
     * Nome do logger desejado
     */
    public BaseController() {
    }

    /**
     * Retorna o contexto JSF
     */
    protected FacesContext getContexto() {
        return FacesContext.getCurrentInstance();
    }

    /**
     * Retorna um bean do escopo JSF
     */
    @SuppressWarnings("deprecation")
    protected Object getBean(final String nome) {
        final FacesContext contexto = getContexto();
        return contexto.getApplication().getVariableResolver().resolveVariable(contexto, nome);
    }

    protected void fatal(final String message) {
        getContexto().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, message, ""));
    }

    protected void error(final String message) {
        getContexto().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, ""));
    }

    protected void info(final String message) {
        getContexto().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, ""));
    }

    protected String getMensagemBundled(String nome) {
        return BeanUtils.getMessageResourceString(getContexto().getApplication().getMessageBundle(), nome, null, getContexto().getViewRoot().getLocale());
    }

}
